package com.village.zpark.controller;

import com.github.pagehelper.PageInfo;
import com.village.zpark.common.ResultMapUtil;
import com.village.zpark.pojo.Farmer;
import com.village.zpark.pojo.Governance;
import com.village.zpark.service.GovernanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/governance")
public class GovernanceController {
    @Autowired
    private GovernanceService governanceService;


    @RequestMapping
    public String Governance(){
        return "/governance";
    }

    @RequestMapping(value = "/governancePage")
    public String governancePage(){
        return "/governancePage";
    }

    @RequestMapping(value = "/governanceQueryById")
    public Object governanceQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        Governance governance = governanceService.findById(id);
        model.addAttribute("obj",governance);
        return "/governancePage";
    }

    @RequestMapping(value = "/governanceList")
    @ResponseBody
    public Object governanceList(){
        List<Governance> governanceList = governanceService.findAll();
        return ResultMapUtil.getHashMapList(governanceList);
    }


    @RequestMapping(value = "/addgovernance")
    @ResponseBody
    public Object addGovernance(Governance governance){
        try {
            governance.setEvaluationtime(new Date());
            int i = governanceService.addGovernance(governance);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/updategovernance")
    @ResponseBody
    public Object updateGovernance(Governance governance){
        try {
            int i = governanceService.updateGovernance(governance);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/deletegovernance")
    @ResponseBody
    public Object deletegovernance(Integer id){
        try {
            int i = governanceService.deleteGovernanceById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/governanceQueryPage")
    @ResponseBody
    public Map<String, Object> farmerQueryPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String param) {
        PageInfo<Governance> governancePageInfo = governanceService.findPage(page, limit, param);
        Map<String,Object> map=new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询数据成功");
        map.put("count", governancePageInfo.getTotal());
        map.put("data", governancePageInfo.getList());
        return map;
    }
}
